import { ColorHex } from "../Const/Constant";
import { PoolManager } from "../Managers/PoolManager";

const { ccclass, property } = cc._decorator;

@ccclass
export class FloatingText extends cc.Component {
    richText: cc.RichText = null!;

    Show(content: string, colorType: ColorHex, isBold: boolean = false, delay: number = 0.35, type: number = 1) {
        this.node.setScale(cc.Vec3.ONE);
        let str = `<color=${colorType}>${content}</c>`;
        if (isBold) str = `<b>${str}</b>`;

        this.richText = this.node.getChildByName(`RichText`).getComponent(cc.RichText);
        this.richText.string = str;
        this.richText.node.opacity = 255;

        cc.Tween.stopAllByTarget(this.node);
        cc.Tween.stopAllByTarget(this.richText.node);

        switch (type) {
            case 1:
                this.ShowTween1(delay);
                break;
            case 2:
                this.ShowTween2(delay);
                break;
            default:
                this.ShowTween1(delay);
                break;
        }
    }

    ShowTween1(delay: number) {
        this.node.setScale(cc.Vec3.ZERO);
        cc.tween(this.node)
            .to(0.2, { scale: 1 }, { easing: `backOut` })
            .delay(delay)
            .call(() => cc.tween(this.richText.node).to(0.25, { opacity: 100 }).start())
            .to(0.25, { position: cc.v3(this.node.position.x, this.node.position.y + 150) })
            .call(() => PoolManager.PutNode(this.node))
            .start();
    }

    ShowTween2(delay: number) {
        cc.tween(this.richText.node).delay(delay).to(0.25, { opacity: 0 }).start();
        cc.tween(this.node).delay(delay).to(0.25, { position: cc.v3(0, 250) }).call(() => { PoolManager.PutNode(this.node); }).start();
    }

}
